<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of address_model
 *
 * @author hassanali
 */
class products_model extends admin_model {

    //put your code here
    public function __construct() {
        parent::__construct();
        $this->table = 'products';
        $this->select = array($this->table . '.*', 'store_details.value', 'maincat.title as maintitle', 'subcat.title as subtitle', 'subcat.additional', 'trades_language.title as tradetitle', 'trades.image as tradeimage', 'stores.owner_id as owner');
        $this->join = array(
            'stores' => 'stores.id = products.store_id',
            'store_details' => 'stores.id = store_details.store_id',
            'categories as maincat' => 'maincat.id = products.mcat_id',
            'categories as subcat' => 'subcat.id = products.subcat_id',
            'trades' => 'trades.id = products.trade_id',
            'trades_language' => 'trades.id = trades_language.trade_id',
        );
        $this->where = array(
            'trades_language.lang' => 'ar',
            'store_details.name' => 'storename',
            'stores.active' => 'YES'
        );
    }

    function get_stat() {
        $pass = array();
        $pass['all_products'] = $this->get(FALSE, TRUE, 'result', FALSE, FALSE, false, false, TRUE);
        $pass['trash_products'] = $this->get(FALSE, TRUE, 'result', FALSE, FALSE, TRUE);
        $pass['active_products'] = $this->get(array('active' => 'YES'), TRUE);
        $pass['notactive_products'] = $this->get(array('active' => 'NO'), TRUE);
        $pass['special_products'] = $this->get(array('special_product' => 'YES'), TRUE);

        $this->db->select('MAX(' . $this->table . '.price) maxprice');
        $temp = $this->db->get($this->table)->row()->maxprice;
        $temp = $this->get(array('price' => $temp), FALSE, 'row');
        $pass['topprice_products'] = '<a href="'.base_url("products/view/$temp->id").'" target="_blank">'.$temp->name.'</a>';
        $pass['topprice_products_price'] = $temp->price;

        $this->db->select('MIN(' . $this->table . '.price) minprice');
        $temp = $this->db->get($this->table)->row()->minprice;
        $temp = $this->get(array('price' => $temp), FALSE, 'row');
        $pass['bottomprice_products'] = '<a href="'.base_url("products/view/$temp->id").'" target="_blank">'.$temp->name.'</a>';
        $pass['bottomprice_products_price'] = $temp->price;

        $this->db->select('MAX(' . $this->table . '.discount_precentage) max');
        $this->db->where('discount_precentage !=', '0');
        $temp = $this->db->get($this->table)->row()->max;
        $temp = $this->get(array('discount_precentage' => $temp), FALSE, 'row');
        $pass['topdiscount_products'] = '<a href="'.base_url("products/view/$temp->id").'" target="_blank">'.$temp->name.'</a>';
        $pass['topdiscount_products_price'] = $temp->discount_precentage;

        $this->db->select('MIN(' . $this->table . '.discount_precentage) min');
        $this->db->where('discount_precentage !=', '0');
        $temp = $this->db->get($this->table)->row()->min;
        $temp = $this->get(array('discount_precentage' => $temp), FALSE, 'row');
        $pass['bottomdiscount_products'] = '<a href="'.base_url("products/view/$temp->id").'" target="_blank">'.$temp->name.'</a>';
        $pass['bottomdiscount_products_price'] = $temp->discount_precentage;
        
        $this->db->select('MAX(' . $this->table . '.view) max');
        $temp = $this->db->get($this->table)->row()->max;
        $temp = $this->get(array('view' => $temp), FALSE, 'row');
        $pass['topview_products'] = '<a href="'.base_url("products/view/$temp->id").'" target="_blank">'.$temp->name.'</a>';
        $pass['topview'] = $temp->view;
        
        $this->db->select('MIN(' . $this->table . '.view) min');
        $temp = $this->db->get($this->table)->row()->min;
        $temp = $this->get(array('view' => $temp), FALSE, 'row');
        $pass['bottomview_products'] = '<a href="'.base_url("products/view/$temp->id").'" target="_blank">'.$temp->name.'</a>';
        $pass['bottomview'] = $temp->view;

        $this->db->group_by('product_id');
        $temp = $this->db->get('invoicie_items')->result();
        $max = 0;
        $min = 1;
        $product_id = 0;
        $product_id_min = 0;
        foreach ($temp as $t) {
            $this->db->where('product_id', $t->product_id);
            $t1 = $this->db->get('invoicie_items')->num_rows();
            if ($t1 == $max) {
                if(!is_array($product_id))
                    $product_id = array();
                $product_id[] = $t->product_id;
            } elseif ($t1 > $max) {
                if(!is_numeric($product_id))
                    $product_id = false;
                $product_id = $t->product_id;
                $max = $t1;
            }elseif ($t1 == $min) {
                if(!is_array($product_id_min))
                    $product_id_min = array();
                $product_id_min[] = $t->product_id;
            } elseif ($t1 < $min) {
                if(!is_numeric($product_id_min))
                    $product_id_min = false;
                $product_id_min = $t->product_id;
                $min = $t1;
            }else{
                if(!is_numeric($product_id_min))
                    $product_id_min = false;
                $product_id_min = $t->product_id;
                $min = $t1;
            }
        }
        $pass['topbuy_products'] = false;
        if (is_array($product_id)) {
            foreach ($product_id as $id)
                $pass['topbuy_products'] .= '<a href="'.base_url("products/view/$id").'" target="_blank">'.get_product_from_id($id)->name . '</a>, ';
        } else {
            $pass['topbuy_products'] = '<a href="'.base_url("products/view/$product_id").'" target="_blank">'.get_product_from_id($product_id)->name.'</a>';
        }
        $pass['topbuy_products_max'] = $max;
        $pass['bottombuy_products'] = false;
        if (is_array($product_id_min)) {
            foreach ($product_id_min as $id)
                $pass['bottombuy_products'] .= '<a href="'.base_url("products/view/$id").'" target="_blank">'.get_product_from_id($id)->name . '</a>, ';
        } else {
            $pass['bottombuy_products'] = '<a href="'.base_url("products/view/$product_id_min").'" target="_blank">'.get_product_from_id($product_id_min)->name.'</a>';
        }
        $pass['bottombuy_products_max'] = $min;
        return $pass;
    }

    function get_advanced($where = array(), $or_like = array()) {
        $return = array();
        if (count($where) > 0) {
            foreach ($where as $key => $value) {
                if (is_array($value)) {
                    $this->db->where_in($this->table . '.' . $key, $value);
                } else {
                    $this->db->where($this->table . '.' . $key, $value);
                }
            }
        }

        if (count($or_like) > 0) {
            foreach ($or_like as $key => $value) {
                if (is_array($value)) {
                    foreach ($value as $k => $v)
                        $this->db->or_like($this->table . '.' . $key, $v);
                } else {
                    $this->db->or_like($this->table . '.' . $key, $value);
                }
            }
        }
        $this->db->group_by($this->table . '.id');
        $return = $this->get();
        return $return;
    }

    function get_products($where = array()) {
        $products = $this->get($where);
        $this->load->model('product_details_model', 'prodetails');
        $nodata = array();
        $index = 0;
        foreach ($products as $product) {
            $details = $this->prodetails->get(array('product_id' => $product->id));
            if (count($details) > 0) {
                $return['prodetails'][$product->id] = $details;
            } else {
                $nodata[] = $index;
            }
            $index++;
        }

        if (count($nodata) > 0) {
            foreach ($nodata as $nd => $va)
                unset($products[$va]);
        }
        $return['products'] = $products;
        return $return;
    }

    function check_if_add($user_id = false) {
        if ($user_id) {
            $this->is_found(array('owner_id' => $user_id), array(), false, 'store');
        }
        else
            return false;
    }

    function aftersave() {
        if (isset($this->data['inv_num']) && isset($this->data['user_id']) && isset($this->data['total']) && isset($this->data['must_pay']) && isset($this->data['title'])) {
            if (empty($this->data['note'])) {
                $this->data['note'] = '';
            }
            $this->db->insert('invoicies', array(
                'inv_num' => $this->data['inv_num'],
                'title' => $this->data['title'],
                'total' => $this->data['total'],
                'must_pay' => $this->data['must_pay'],
                'note' => $this->data['note'],
                'for' => $this->data['for'],
            ));
        }
    }

}